package cn.zhiwen.util;

import org.apache.log4j.Logger;

import java.util.Date;
import java.util.Random;

/**
 * 加载定时任务
 * @Description
 * @author qgw
 * 2016 下午2:24:58 ^_^
 */
@SuppressWarnings("unchecked")
public class LoadTask {
	private static final Logger log = Logger.getLogger("");
	/**
	 * @param sendTime 发送时间 
	 * @return
	 * @author qgw 
	 * @date 2016年1月26日 下午3:39:13 ^_^
	 */
	public static boolean timerTask(Date sendTime, long msgId) {
		String cron = DateUtil.getCron(sendTime);//获得quartz时间表达式，此方法自己写
		ScheduleJob job = new ScheduleJob();
		String jobName = msgId+"======"+new Random().nextInt()+"";
//		job.setJobId(msgId);
//		job.setJobName(jobName);
//		job.setCreTime(nowTime);
//		job.setJobCron(cron);
//		job.setJobTime(sendTime);
//		job.setJobGroup("MY_JOBGROUP_NAME");
//		job.setJobDesc(desc);
        job.setId(jobName);
        job.setCronExpression(cron);
		try {
			//删除已有的定时任务
			QuartzManager.removeJob(jobName);
			//添加定时任务
			QuartzManager.addJobs(jobName, QuartzJobFactory.class, cron,job);
			return true;
		} catch (Exception e) {
			log.info("加载定时器错误："+e);
			return false;
		}
	}

	public static boolean timerTask(String sendTime, long msgId) {
		ScheduleJob job = new ScheduleJob();
		String jobName = msgId+"======"+new Random().nextInt()+"";
//		job.setJobId(msgId);
//		job.setJobName(jobName);
//		job.setCreTime(nowTime);
//		job.setJobCron(cron);
//		job.setJobTime(sendTime);
//		job.setJobGroup("MY_JOBGROUP_NAME");
//		job.setJobDesc(desc);
		job.setId(jobName);
		job.setCronExpression(sendTime);
		try {
			//删除已有的定时任务
			//QuartzManager.removeJob(jobName);
			//添加定时任务
			QuartzManager.addJobs(jobName, QuartzJobFactory.class, sendTime,job);
			return true;
		} catch (Exception e) {
			log.info("加载定时器错误："+e);
			return false;
		}
	}
}